﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;

public partial class Book : System.Web.UI.Page
{
    private static int orderID;
    private static int tourID;
    private static int cusID;
    //private int expireTime = 15;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["tourID"] != null)
            {
                //Check session and cookie for this Customer
                if (Session["cusID"] != null)
                {
                    cusID = int.Parse(Session["cusID"].ToString());
                }
                //else
                //{
                //    // check cookie 
                //    if (Request.Cookies["cusID"] != null)
                //    {
                //        cusID = int.Parse(Request.Cookies["cusID"].Value);
                //    }

                //}
                if (cusID != null && cusID > 0)
                {
                    //Load data to from book now
                    DataTable dtCus = CusManagement.GetCusByID(cusID);
                    if (dtCus.Rows.Count == 1)
                    {
                        txtName.Text = dtCus.Rows[0]["cusName"].ToString();
                        txtTel.Text = dtCus.Rows[0]["tel"].ToString();
                        txtEmail.Text = dtCus.Rows[0]["email"].ToString();
                    }
                }

                tourID = int.Parse(Request.QueryString["tourID"].ToString());
                DataTable dtTourInfo = TourManagement.GetTourByID(tourID);
                dlTourInfo.DataSource = dtTourInfo;
                dlTourInfo.DataBind();

                MultiView1.ActiveViewIndex = 0;
            }
            else
            {
                //Check session and cookie for this Customer
                if (Session["cusID"] != null)
                {
                    cusID = int.Parse(Session["cusID"].ToString());
                }
                //else
                //{
                //    // check cookie 
                //    if (Request.Cookies["cusID"] != null)
                //    {
                //        cusID = int.Parse(Request.Cookies["cusID"].Value);
                //    }

                //}

                LoadTourByCus();

                MultiView1.ActiveViewIndex = 1;
            }
        }
    }

    private void LoadTourByCus()
    {
        DataTable dt = OrderTourManagement.GetOrderByCus(cusID).Tables[0];
        DataColumn newColumn = new DataColumn("numRoom");
        dt.Columns.Add(newColumn);
        foreach (DataRow row in dt.Rows)
        {
            double adult = double.Parse(row["numAdult"].ToString());
            double child = double.Parse(row["numChild"].ToString());
            double room = Math.Ceiling(adult / Common.ADULT_PER_ROOM + child / Common.CHILD_PER_ROOM);
            row["numRoom"] = room;
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        int cusIDNew = -1;
        //Check Customer register?
        if (cusID <= 0)
        {
            if (Session["cusID"] == null)   // || Request.Cookies["cusID"] == null)
            {
                //Register Customer
                cusIDNew = CusManagement.AddCus(txtName.Text, txtTel.Text, txtEmail.Text);
                //Store cusID to session and cookie for next review
                Session["cusID"] = cusIDNew;
                //if (Request.Cookies["cusID"] == null)
                //{
                //    HttpCookie cookie = new HttpCookie("cusID", cusIDNew.ToString());
                //    cookie.Expires = DateTime.Today.AddDays(expireTime);
                //    Response.Cookies.Add(cookie);
                //}
                //Register OrderTour
                OrderTourManagement.AddOrderTour(tourID, cusIDNew, int.Parse(ddlAdult.SelectedValue), int.Parse(ddlChild.SelectedValue));
            }
        }
        else
        {
            if (cusID ==null && Session["cusID"] != null)
                cusID = int.Parse(Session["cusID"].ToString());
            //else
            //{
            //    if (cusID == null && Request.Cookies["cusID"] != null)
            //        cusID = int.Parse(Request.Cookies["cusID"].Value);
            //}
            //Register OrderTour
            OrderTourManagement.AddOrderTour(tourID, cusID, int.Parse(ddlAdult.SelectedValue), int.Parse(ddlChild.SelectedValue));
        }
        
        ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('We will contact to you soon!');</script>");

        Response.Redirect("Book.aspx");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Tours.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadTourByCus();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        orderID = int.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
        DataTable dtTour = OrderTourManagement.GetOrderByID(orderID);
        if (dtTour.Rows.Count > 0)
        {
            lblCusName.Text = dtTour.Rows[0][CusManagement.CUSNAME].ToString();
            DropDownList1.SelectedValue = dtTour.Rows[0][OrderTourManagement.NUMADULT].ToString();
            DropDownList2.SelectedValue = dtTour.Rows[0][OrderTourManagement.NUMCHILD].ToString();
            lblTel.Text = dtTour.Rows[0][CusManagement.TEL].ToString();
            lblEmail.Text = dtTour.Rows[0][CusManagement.EMAIL].ToString();
            lblTourCode.Text = dtTour.Rows[0][TourManagement.TOURCODE].ToString();
            lblTourName.Text = dtTour.Rows[0][TourManagement.TOURNAME].ToString();
            lblPlaceStart.Text = dtTour.Rows[0][TourManagement.PLACESTART].ToString();
            lblPlaceEnd.Text = dtTour.Rows[0][TourManagement.PLACEEND].ToString();
            lblDateStart.Text = DateTime.Parse(dtTour.Rows[0][TourManagement.STARTDATE].ToString()).ToShortDateString();
            lblDateEnd.Text = DateTime.Parse(dtTour.Rows[0][TourManagement.ENDDATE].ToString()).ToShortDateString();
            lblItinerary.Text = dtTour.Rows[0][TourManagement.ITINERARY].ToString();
            lblPlan.Text = dtTour.Rows[0][TourManagement.PLANTOUR].ToString();
        }

        MultiView1.ActiveViewIndex = 2;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Tours.aspx");
    }
}